package com.kakarote.admin.controller;


import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.admin.entity.BO.AdminDeptBO;
import com.kakarote.admin.entity.BO.AdminDeptQueryBO;
import com.kakarote.admin.entity.PO.AdminDept;
import com.kakarote.admin.entity.VO.AdminDeptRoleVO;
import com.kakarote.admin.entity.VO.AdminDeptVO;
import com.kakarote.admin.service.IAdminDeptService;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.ApiExplain;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.PageEntity;
import com.kakarote.core.entity.QueryParentDeptBO;
import com.kakarote.core.feign.admin.entity.SimpleDept;
import com.kakarote.core.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-04-27
 */
@RestController
@RequestMapping("/adminDept")
@Api(tags = "部门管理相关接口")
public class AdminDeptController {

    @Resource IAdminDeptService adminDeptService;

    @PostMapping("/queryDeptTree")
    @ApiOperation("查询部门列表树")
    public Result<List<AdminDeptVO>> queryDeptTree(@RequestBody AdminDeptQueryBO queryBO) {
        List<AdminDeptVO> deptList = adminDeptService.queryDeptTree(queryBO);
        return Result.ok(deptList);
    }

    @PostMapping("/queryDeptByRole/{roleId}")
    @ApiOperation("通过角色查询部门列表")
    public Result<BasePage<AdminDeptRoleVO>> queryDeptByRole(@PathVariable Long roleId, PageEntity page) {
        return Result.ok(adminDeptService.queryDeptByRole(roleId, page));
    }


    @PostMapping("/addDept")
    @ApiOperation("新增部门")
    @OperateLog(apply = ApplyEnum.SYSTEM_SETTING, behavior = BehaviorEnum.SAVE, object = OperateObjectEnum.EMPLOYEE_DEPARTMENT_MANAGEMENT, type = OperateTypeEnum.SETTING)
    public Result addDept(@RequestBody @Valid AdminDeptBO adminDept) {
        OperationLog operationLog = adminDeptService.addDept(adminDept);
        return OperationResult.ok(ListUtil.toList(operationLog));
    }

    @PostMapping("/setDept")
    @ApiOperation("修改部门")
    @OperateLog(apply = ApplyEnum.SYSTEM_SETTING, behavior = BehaviorEnum.UPDATE, object = OperateObjectEnum.EMPLOYEE_DEPARTMENT_MANAGEMENT, type = OperateTypeEnum.SETTING)
    public Result setDept(@RequestBody @Valid AdminDeptBO adminDept) {
        OperationLog operationLog = adminDeptService.setDept(adminDept);
        return OperationResult.ok(ListUtil.toList(operationLog));
    }

    @ApiOperation("设置部门排序")
    @PostMapping("/setDeptSort")
    public Result setDeptSort(@RequestBody List<Long> deptIds) {
        adminDeptService.setDeptSort(deptIds);
        return Result.ok();
    }


    @PostMapping("/deleteDept/{deptId}")
    @ApiOperation("删除部门")
    @OperateLog(apply = ApplyEnum.SYSTEM_SETTING, behavior = BehaviorEnum.DELETE, object = OperateObjectEnum.EMPLOYEE_DEPARTMENT_MANAGEMENT, type = OperateTypeEnum.SETTING)
    public Result deleteDept(@PathVariable("deptId") Long deptId) {
        OperationLog operationLog = adminDeptService.deleteDept(deptId);
        return OperationResult.ok(ListUtil.toList(operationLog));
    }

    @RequestMapping("/queryChildDeptId")
    @ApiExplain("根据部门ID下的子部门")
    public Result<List<Long>> queryChildDeptId(@NotNull Long deptId) {
        return Result.ok(adminDeptService.queryChildDept(deptId));
    }

    @PostMapping("/queryDeptByIds")
    @ApiExplain("根据部门ID获取用户")
    public Result<List<SimpleDept>> queryDeptByIds(@RequestBody List<Long> ids) {
        return Result.ok(adminDeptService.queryDeptByIds(ids));
    }

    @RequestMapping("/queryDeptList")
    @ApiExplain("获取全部部门")
    public Result<List<SimpleDept>> queryDeptList() {
        return Result.ok(adminDeptService.queryDeptList());
    }


    @PostMapping("/queryParentDeptIdByLevel")
    @ApiExplain("获取指定部门的上级部门ID")
    Result<List<Long>> queryParentDeptIdByLevel(@RequestBody QueryParentDeptBO queryBO) {
        return Result.ok(adminDeptService.queryParentDeptIdByLevel(queryBO));
    }

    @GetMapping("/querySameLevelDeptId")
    @ApiExplain("获取同层级部门")
    Result<List<Long>> querySameLevelDeptId(@RequestParam("deptId") Long deptId) {
        return Result.ok(adminDeptService.querySameLevelDeptId(deptId));
    }

    @PostMapping("/queryDeptIsExistRole")
    @ApiOperation("获取部门是否存在指定角色")
    Result<Boolean> queryDeptIsExistRole(@RequestParam("deptId") Long deptId, @RequestParam("label") Long label){
       return Result.ok(adminDeptService.queryDeptIsExistRole(deptId,label));
    }

}

